package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/special/model"
)

type SpecialBuyMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBuyMapper) CountUserBuyNote(noteId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_user_buy WHERE user_id = ? AND system_id = ? AND opid in (
		SELECT DISTINCT special_id  FROM xk_special_bind_detail WHERE note_id = ? AND system_id = ?
	)`, userId, systemId, noteId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题明细
 */
func (mapper *SpecialBuyMapper) CountUserBindBuyNote(noteId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id = ? AND system_id = ? AND opid IN
	(SELECT bind_id  FROM xk_special_bind_detail WHERE note_id = ? AND system_id = ?)`, userId, systemId, noteId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBuyMapper) CountUserBuyCourse(courseId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_user_buy WHERE user_id = ? AND system_id = ? AND opid in (
		SELECT DISTINCT special_id  FROM xk_special_bind_detail WHERE course_id = ? AND system_id = ?
	)`, userId, systemId, courseId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题明细
 */
func (mapper *SpecialBuyMapper) CountUserBindBuyCourse(courseId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id = ? AND system_id = ? AND opid IN
	(SELECT bind_id  FROM xk_special_bind_detail WHERE course_id = ? AND system_id = ?)`, userId, systemId, courseId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题
 */
func (mapper *SpecialBuyMapper) CountUserBuyDownloads(downloadId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_user_buy WHERE user_id = ? AND system_id = ? AND opid in (
		SELECT DISTINCT special_id  FROM xk_special_bind_detail WHERE download_id = ? AND system_id = ?
	)`, userId, systemId, downloadId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题明细
 */
func (mapper *SpecialBuyMapper) CountUserBindBuyDownloads(downloadId uint64, userId uint64, systemId uint64) (int32, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int32
	err := db.Raw(`SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id = ? AND system_id = ? AND opid IN
	(SELECT bind_id  FROM xk_special_bind_detail WHERE download_id = ? AND system_id = ?)`, userId, systemId, downloadId, systemId).QueryRow(&total)
	// 4: 结构返回
	return total, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题明细
 */
func (mapper *SpecialBuyMapper) CountUserBindBuySpecialBind(bindId uint64, specialId uint64, userId uint64, systemId uint64) int {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	var total int
	_ = db.Raw(`SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id = ? AND special_id = ? AND system_id = ? AND opid = ?`, userId, specialId, systemId, bindId).QueryRow(&total)
	// 4: 结构返回
	return total
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 购买了专题明细
 */
func (mapper *SpecialBuyMapper) FindSpecialUserBuyStudents(specialId uint64, systemId uint64) ([]model.UserBuySpecial, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	students := []model.UserBuySpecial{}
	_, _ = db.Raw(`SELECT id,uuid,avatar,nickname,username,user_id FROM xk_special_user_buy WHERE  opid = ? AND system_id = ?`, specialId, systemId).QueryRows(&students)
	// 4: 结构返回
	return students, nil
}
